package main

import (
	// "bgs/cache"
	// "bgs/component"
	"bp/conf"
	// "fmt"
	// "fmt"
	// "bp/internal/util"

	// "bgs/grpc/client"
	// "bp/internal/nsqserver"
	"bp/internal/nsqserver"
	"bp/internal/rpcserver"

	// "bgs/nsqp"
	"flag"
	"os"
	"path/filepath"

	// "github.com/google/uuid"
	"github.com/spf13/pflag"
	"github.com/spf13/viper"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

var (
	grpcConfPtr *conf.GrpcSection
	mwebConfPtr *conf.MWebSection
	webConfPtr  *conf.WebSection
	pgConfPtr   *conf.PgSection
	// wechatConfPtr  *conf.WechatSection
	nsqConfPtr *conf.NsqSection
	// notifierConf   conf.NotifierSection
	// wxpayConf      conf.WxpaySection
	// inventoryConf  conf.InventorySection
	// accountConf    conf.AccountSection
	// bilinAdminConf conf.BilinAdminSection
	bilinProdConfPtr *conf.BilinProdSection
	varsConfPtr      *conf.VarsSection
	// flagC         string
	slog *zap.SugaredLogger
)

func main() {
	initLog()
	readConfig()

	slog.Debugf("pgConf => %+v", *pgConfPtr)
	slog.Debugf("grpcConf => %+v", *grpcConfPtr)
	slog.Debugf("mwebConf => %+v", *mwebConfPtr)
	slog.Debugf("webConf => %+v", *webConfPtr)
	slog.Debugf("nsqConf => %+v", *nsqConfPtr)
	// slog.Debugf("notifierConf => %+v", notifierConf)
	// slog.Debugf("wechatConf => %+v", *wechatConfPtr)
	// slog.Debugf("wxpayConf => %+v", wxpayConf)
	// slog.Debugf("inventoryConf => %+v", inventoryConf)
	// slog.Debugf("accountConf => %+v", accountConf)
	// slog.Debugf("bilinAdminConf => %+v", bilinAdminConf)
	slog.Debugf("bilinProdConf => %+v", *bilinProdConfPtr)
	slog.Debugf("varsConf => %+v", *varsConfPtr)

	// adp := cache.NewAdapter(pgdao, slog)

	// componentManger := component.InitComponentManger(slog, pgdao, varsConfPtr, adp, notifierConf, wechatConfPtr)
	// if componentManger != nil {
	// 	slog.Infof("components ready")
	// }
	// for i := 0; i < 100; i++ {
	// 	chTest := componentManger.Notifier.GetTestWorker()
	// 	chTest <- i
	// }

	// for i:=0; i< 10;i++ {
	// 	val, err := uuid.NewUUID()
	// 	if err == nil {
	// 		fmt.Println(val.String())
	// 	}
	// }
	// val, _ := uuid.NewUUID()
	// vals := util.SplitString(val.String(), "-")
	// no := fmt.Sprintf("%s%s", vals[0], vals[3])
	// fmt.Println(no)

	// clientProxy := client.NewGrpcClientProxy(slog, varsConfPtr, accountConf, bilinAdminConf, bilinProdConf, inventoryConf, wxpayConf)
	err := nsqserver.InitNsqServer(slog, pgConfPtr, nsqConfPtr, varsConfPtr)
	if err != nil {
		slog.Errorf("nsq server init err: %s", err.Error())
	}
	// grpcImp.InitGRPC(grpcConf, wechatConfPtr, varsConfPtr, pgdao, qn, slog, adp, clientProxy, componentManger)
	rpcserver.InitRpcServer(slog, pgConfPtr, varsConfPtr, grpcConfPtr, nsqConfPtr, mwebConfPtr, webConfPtr)
}

func initLog() {
	c := zap.NewProductionEncoderConfig()
	c.EncodeTime = zapcore.ISO8601TimeEncoder
	c.TimeKey = "time"
	core := zapcore.NewCore(
		zapcore.NewJSONEncoder(c),
		zapcore.AddSync(os.Stdout),
		zap.NewAtomicLevelAt(zap.InfoLevel))
	logger := zap.New(core, zap.AddStacktrace(zapcore.FatalLevel), zap.AddCaller())
	defer logger.Sync()
	zap.ReplaceGlobals(logger)
	// logger.Info("initlog", zap.String("name", "hhhhhh"))
	slog = zap.S()
	slog.Infof("global SugaredLogger after %s", "bgs")
}

func readConfig() {
	flag.String("c", "conf/config.toml", "set configuration `file`")
	pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
	//解析flag
	pflag.Parse()
	//viper绑定pflag
	viper.BindPFlags(pflag.CommandLine)
	viper.SetConfigType("toml")
	configValue := viper.GetString("c")
	viper.AddConfigPath(filepath.Dir(configValue))
	viper.SetConfigName(filepath.Base(configValue))
	// if (configValue != "") {
	// 	slog.Info("use -c")
	// 	viper.AddConfigPath(filepath.Dir(configValue))
	// 	viper.SetConfigName(filepath.Base(configValue))
	// } else {
	// 	slog.Info("not use -c")
	// 	viper.SetConfigName("config")
	// 	viper.SetConfigType("toml")
	// 	viper.AddConfigPath("./")
	// 	viper.AddConfigPath("./conf")
	// 	viper.AddConfigPath("../conf")
	// 	viper.AddConfigPath("../../conf")
	// }

	err := viper.ReadInConfig()
	if err != nil {
		slog.Panicf("Fatal error config file: %s \n", err)
	}

	pgConf := conf.ReadPg(viper.Sub("pg"), slog)
	pgConfPtr = &pgConf
	grpcConf := conf.ReadGRPC(viper.Sub("grpc"), slog)
	grpcConfPtr = &grpcConf
	mwebConf := conf.ReadMWeb(viper.Sub("mweb"), slog)
	mwebConfPtr = &mwebConf
	webConf := conf.ReadWeb(viper.Sub("web"), slog)
	webConfPtr = &webConf
	// wechatConf := conf.ReadWechat(viper.Sub("wechat"), slog)
	// wechatConfPtr = &wechatConf
	// qiniuConf = conf.ReadQiniu(viper.Sub("qiniu"), slog)
	nsqConf := conf.ReadNsq(viper.Sub("nsq"), slog)
	nsqConfPtr = &nsqConf
	if nsqdAddressFromEnv := os.Getenv(conf.NSQD_ADDRESS_KEY); nsqdAddressFromEnv != "" {
		nsqConfPtr.NsqdAddress = nsqdAddressFromEnv
	}
	// notifierConf = conf.ReadNotifier(viper.Sub("notifier"), slog)
	// wxpayConf = conf.ReadWxpay(viper.Sub("wxpay"), slog)
	// inventoryConf = conf.ReadInventory(viper.Sub("inventory"), slog)
	// accountConf = conf.ReadAccount(viper.Sub("account"), slog)
	// bilinAdminConf = conf.ReadBilinAdmin(viper.Sub("bilin-admin"), slog)
	bilinProdConf := conf.ReadBilinProd(viper.Sub("bilin-prod"), slog)
	bilinProdConfPtr = &bilinProdConf

	varsConf := conf.ReadVars(viper.Sub("vars"), slog)
	varsConfPtr = &varsConf
}
